﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Repository;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 仓位
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class CellController : BaseController
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly ICellService cellService;
        private readonly ICellRepository cellRepository;
        public CellController(IWebHostEnvironment environment, ICellService cellService, ICellRepository cellRepository, IUnitOfWork unitOfWork) : base(environment)
        {
            this.unitOfWork = unitOfWork;
            this.cellService = cellService;
            this.cellRepository = cellRepository;
        }
        /// <summary>
        /// 添加仓位
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Addell(Cell obj)
        {
            if (string.IsNullOrEmpty(obj.Code) || obj.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string message = "";
            obj.CreateUserId = base.BaseGetNowUserId();
            obj.CreateUserName = base.BaseGetNowUserName();
            bool result = cellService.AddCell(obj, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }
        /// <summary>
        /// 修改仓位
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdCell(Cell obj)
        {
            if (obj.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(obj.Code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            obj.ModifyUserId = base.BaseGetNowUserId();
            obj.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            bool result = cellService.UpdCell(obj, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！" + message, "200"));
        }
        /// <summary>
        /// 删除仓位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelCell(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }

            bool IsOK = cellService.DelCell(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 仓位左侧树形显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuitemList()
        {
            var mlist = cellService.GetMenuitem();

            if (mlist != null)
            {
                return Ok(BaseResult.Result(mlist, "获取成功！"));
            }

            return Ok(BaseResult.Result(null, "获取失败！"));
        }

        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="logicalPartCode"></param>
        /// <param name="fullStatus"></param>
        /// <param name="status"></param>
        /// <param name="cname"></param>
        /// <param name="whid"></param>
        /// <param name="areid"></param>
        /// <param name="sheid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCellList(string? logicalPartCode = "", int fullStatus = 3, int status = 8, string? cname = "", long whid = 0, long areid = 0, long sheid = 0, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = cellService.GetCellList(logicalPartCode, fullStatus, status, cname, whid, areid, sheid, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCellById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var cobj = cellService.GetCellById(id);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 根据code获取对象
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCellByCode(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            var cobj = cellService.GetCellByCode(code);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 批量生成仓位
        /// </summary>
        /// <param name="cellCreateBodyDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BatchCreateCell(CellCreateBodyDTO cellCreateBodyDTO)
        {
            if (cellCreateBodyDTO.WhId.GetValueOrDefault() <= 0 || cellCreateBodyDTO.AreaId.GetValueOrDefault() <= 0 || cellCreateBodyDTO.ShelfIds == null || cellCreateBodyDTO.ShelfIds.Count <= 0)
            {
                return Ok(BaseResult.Result(false, "未选择库房或库区或货架", "500"));
            }
            if (cellCreateBodyDTO.MaxLayer <= 0)
            {
                return Ok(BaseResult.Result(false, "最大层数未设置", "500"));
            }
            if (cellCreateBodyDTO.MaxColumn <= 0)
            {
                return Ok(BaseResult.Result(false, "最大列数未设置", "500"));
            }
            string message = "";
            var isOk = cellService.BatchCreateCell(cellCreateBodyDTO, out message);
            if (isOk)
            {
                return Ok(BaseResult.Result(true, "批量添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, message, "500"));
        }

        /// <summary>
        /// 枚举高度类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmHeightType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmHeightType>();

            return Ok(list);
        }

        /// <summary>
        /// 仓位统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCellCount()
        {
            var cobj = cellService.GetCellCount();
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加移库单时获取同巷道仓位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTransferBinToCellList(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = cellService.GetTransferBinToCellList(id);
            return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
        }

        /// <summary>
        /// 将仓位转换成010101
        /// </summary>
        /// <param name="arecode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RepairCell(string arecode)
        {
            if (string.IsNullOrEmpty(arecode))
            {
                return Ok(BaseResult.Result("400", "库区编码有误！"));
            }
            var cell = cellRepository.GetEntityWhereAll(x => x.AreaCode == arecode).ToList();
            foreach (var item in cell)
            {
                string[] parts = item.Code.Substring(5).Split(separator: '_');
                int rou = Convert.ToInt32(parts[0]); // 巷道是第一个数字
                int row = Convert.ToInt32(parts[1]);    // 排是第二个数字
                int col = Convert.ToInt32(parts[2]);    // 列是第三个数字
                int lev = Convert.ToInt32(parts[3]);  // 层是第
                item.LineNo = row;
                item.LayerNo = lev;
                item.Code = row.ToString("D2") + col.ToString("D2") + lev.ToString("D2");
                cellRepository.UpdEntity(item);
            }
            return Ok(unitOfWork.SaveChangesEntity());
        }
    }
}
